package org.kwisatz.proxybot.core;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.kwisatz.proxybot.FilterAdapter;
import org.kwisatz.proxybot.User;
import org.kwisatz.proxybot.Message;

public class ChanManager extends FilterAdapter
{	
	private final static Pattern PATTERN_WHOR=Pattern.compile("\\S+ (\\S+) (\\S+) (\\S+) (\\S+) (\\S+) \\S+");		
	
	@Override
	public String handleServerLine(String line, Message message)
	{
		if(message==null)
			return line;
		
		//START WHO
		if("WHO".equals(message.getCommand()))
		{
			((ContextImpl)context).resetChan(message.getCmdParam());
			return line;
		}
		
		//WHO response
		//:irc.chat-land.org 352 KwisatzHaderach #Informatique j2 Helper.chat-land.org irc.chat-land.org DJ-PasLa H :0 15 H Belgique
		if("352".equals(message.getCommand()))
		{
			Matcher m = PATTERN_WHOR.matcher(message.getCmdParam());
			if(m.matches())
			{
				String chan=m.group(1);
				String login=m.group(2);
				String host=m.group(3);
				//String server=m.group(4);
				String nick=m.group(5);
				String realname=message.getEndOfMsg();
				User u=new UserImpl(login, host, nick, realname);
				((ContextImpl)context).addUser(chan, u);
			}
			return line;
		}
		
		//:Thorkal!j2@X-CD606439.w83-115.abo.wanadoo.fr JOIN :#Informatique
		if("JOIN".equals(message.getCommand()))
		{
			String chan=message.getEndOfMsg();			
			((ContextImpl)context).addUser(chan, createUser(message));
			return line;
		}

		//[18:37:02] KICK #java Own3d
		//[18:37:02] :KwisatzHaderach!j2@Helper.chat-land.org KICK #java Own3d :KwisatzHaderach
		//[18:37:02] :Own3d!j@A361D682.D9066255.D7892185.IP JOIN :#java
		if("KICK".equals(message.getCommand()))
		{
			String s=message.getCmdParam();
			int i=s.indexOf(' ');
			String nick=s.substring(i+1);
			String chan=s.substring(0, i-1);
			if(nick.equals(context.getNickName()))
				((ContextImpl)context).resetChan(chan);
			else
				((ContextImpl)context).removeUser(chan, new UserImpl(null, null, nick, null));
			return line;
		}
		
		//:Thorkal!j2@X-CD606439.w83-115.abo.wanadoo.fr PART #Informatique
		if("PART".equals(message.getCommand()))
		{
			String chan=message.getCmdParam();			
			if(message.getNick().equals(context.getNickName()))
				((ContextImpl)context).resetChan(chan);
			else
				((ContextImpl)context).removeUser(chan, createUser(message));
			return line;
		}
		
		//:raser!j@X-8C2B8634.w90-13.abo.wanadoo.fr QUIT :Connection reset by peer
		if("QUIT".equals(message.getCommand()))
		{
			User u = createUser(message);
			String[] chans = context.getChannels();
			for (String chan : chans) {
				((ContextImpl) context).removeUser(chan, u);
			}
		}
		//:KwisatzHaderach!j2@X-16DD7B7C.fbx.proxad.net NICK :KwizatsHaderach
		if("NICK".equals(message.getCommand()))
		{
			User uOld=createUser(message);							
			User uNew=new UserImpl(message.getUser(), message.getServer(), message.getEndOfMsg(), null);						
			String[] chans=context.getChannels();
			for (String chan : chans) {
				((ContextImpl) context).removeUser(chan, uOld);
				((ContextImpl) context).addUser(chan, uNew);
			}

			return line;
		}
		
		
		//END WHO
		/*if(message.getCommand().equals("315"))
		{
			displayContext();
		}*/
		return line;
	}

	private static User createUser(Message message)
	{
		return new UserImpl(message.getUser(),
			message.getServer(), message.getNick(), null);
	}
	
	/*private void displayContext() {
		String[] chans=context.getChannels();
		for(int i=0; i<chans.length; i++)
		{
			User[] us=context.getUsers(chans[i]);
			for(int j=0; j<us.length; j++)
			{
				System.out.println(us[j]);
			}
		}
	}*/
}
